package admin.models;

import helpers.GeneralHelper;
import helpers.StringHelper;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import javax.cache.Cache;
import javax.cache.CacheException;
import javax.cache.CacheManager;

import com.google.appengine.api.NamespaceManager;
import com.googlecode.objectify.Key;
import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.ObjectifyOpts;
import com.googlecode.objectify.ObjectifyService;
import com.googlecode.objectify.Query;

import entities.Category;

public class CategoryModel {
	
	private Objectify ofy;

	private Cache cache = null;

	private String cachePrefix = "category_";

	private static CategoryModel instance = null;
	
	public static CategoryModel getInstance() {
		if (instance == null) {
			instance = new CategoryModel();
		}
		
		return instance;
	}
	
	private CategoryModel() {
		try {
			ObjectifyService.register(Category.class);
		} catch (Exception e) {
			// e.printStackTrace();
		}
		cache = GeneralHelper.getCache();
		ofy = GeneralHelper.getObjectify();
	}

	public List<Category> list(String ns, int limit, int page) {
		String currentNs = NamespaceManager.get();
		if (!currentNs.equals(ns)) {
			NamespaceManager.set(ns);
		}
		
		List<Category> categories = new ArrayList<Category>();

		int start = (page - 1) * limit;
		Query<Category> q = ofy.query(Category.class).limit(limit)
				.offset(start);
		for (Category category : q) {
			categories.add(category);
		}

		NamespaceManager.set(currentNs);
		
		return categories;
	}
	
	public void insert(String ns, Category category) {
		String currentNs = NamespaceManager.get();
		if (!currentNs.equals(ns)) {
			NamespaceManager.set(ns);
		}
		
		ofy.put(category);
		
		NamespaceManager.set(currentNs);
		
	}

	public void delete(String ns, Category category) {
		String currentNs = NamespaceManager.get();
		if (!currentNs.equals(ns)) {
			NamespaceManager.set(ns);
		}
		
		try {
			cache.remove(cachePrefix + "ns_" + ns + "_" + category.getAlias());
		} catch (Exception e) {
			// TODO: handle exception
		}

		ofy.delete(category);
		
		NamespaceManager.set(currentNs);
	}

	
	public List<Category> getAll(String ns) {
		String currentNs = NamespaceManager.get();
		if (!currentNs.equals(ns)) {
			NamespaceManager.set(ns);
		}
		
		List<Category> categories = new ArrayList<Category>();
		
		String cacheName = cachePrefix + "ns_" + ns + "_list_all";
		boolean isCached = false;
		
		if (cache != null) {
			try {
				categories = (List<Category>) cache.get(cacheName);
				if (categories != null) isCached = true;
			} catch (Exception e) {
			}			 
		}
		
		if (!isCached) {
			categories = ofy.query(Category.class).list();
			
			if (cache != null) {
				cache.put(cacheName, categories);
			}
		}
		
		NamespaceManager.set(currentNs);
		
		return categories;
	}
	
	public Map<String, Category> getMapAll(String ns) {
		
		Map<String, Category> categories = new TreeMap<String, Category>();
		
		List<Category> c = getAll(ns);
		
		for (int i = 0; i < c.size(); i++) {
			if (c.get(i) != null) {
				categories.put(c.get(i).getId(), c.get(i));
			}
		}
		
		
		return categories;
	}

	public Category getCategory(String ns, String id) {
		String currentNs = NamespaceManager.get();
		if (!currentNs.equals(ns)) {
			NamespaceManager.set(ns);
		}
		
		Category category = new Category();
		
		try {
			category = ofy.get(new Key<Category>(Category.class, id));
		} catch (Exception e) {
			category = null;
		}
		
		NamespaceManager.set(currentNs);
		
		return category;
	}

	public Category getCategoryByAlias(String ns, String alias) {
		Category category = new Category();
		
		String currentNs = NamespaceManager.get();
		if (!currentNs.equals(ns)) {
			NamespaceManager.set(ns);
		}
		
		String cacheName = cachePrefix + "ns_" + ns + "_alias_" + alias;
		boolean isCached = false;
		
		if (cache != null) {
			try {
				category = (Category) cache.get(cacheName);
				if (category != null) isCached = true;
			} catch (Exception e) {
			}
		} 
		
		if (!isCached) {
			try {
				category = ofy.query(Category.class).filter("alias", alias).get();
				if (cache != null && category != null) {
					cache.put(cacheName, category);
				}
			} catch (Exception e) {
				category = null;
			}
				
		}
		
		NamespaceManager.set(currentNs);
		
		return category;
	}
	
	public Map<String, Category> getByListId(String ns, List<String> ids) {
		String currentNs = NamespaceManager.get();
		if (!currentNs.equals(ns)) {
			NamespaceManager.set(ns);
		}
		
		
		Map<String, Category> categories = new TreeMap<String, Category>();
		
		categories = ofy.get(Category.class, ids);
		
		NamespaceManager.set(currentNs);
		
		return categories;
	}
	
	public boolean checkAliasExists(String ns, String alias) {
		boolean isExists = false;
		
		Category category = getCategoryByAlias(ns, alias);
		
		if (category != null) {
			isExists = true;
		}
		
		return isExists;
	}
	
}
